<?php

use Illuminate\Support\Facades\Redis;
use App\Models\LaravelFastApi\V1\Admin\Admin;
use App\Models\LaravelFastApi\V1\User\Union\UserRoleUnion;
use App\Models\LaravelFastApi\V1\System\Role\Role;

if(!function_exists('getAdminRoles'))
{
   /**
	* 获取管理员角色列表
	* 
	* 先从Redis缓存中获取管理员角色，如果不存在则从数据库查询并缓存到Redis
	* 
	* @param Admin $admin 管理员对象
	* @return array 返回管理员角色逻辑名称数组
	*/
   function getAdminRoles(Admin $admin)
   {
	   $redisRoles = Redis::hget("admin_roles:admin_roles",$admin->id);

	   $rolsArray = [];

	   if($redisRoles)
	   {
		  $rolsArray = json_decode($redisRoles,true);
	   }
	   else
	   {

		  //查询管理员的用户角色关联
		  $adminRoleUnionCollection = UserRoleUnion::where('user_id',$admin->user_id)->get();

		  $roleIdArray = [];

		  foreach ($adminRoleUnionCollection as $key => &$adminRoleUnionItem) 
		  {
			  array_push($roleIdArray,$adminRoleUnionItem->role_id);
		  }

		  array_unique($roleIdArray);

		  //获取角色集合
		  $roleCollection = Role::whereIn('id',$roleIdArray)->get();

		  $roleLogicNameArray = [];

		  foreach ($roleCollection as $key => $roleItem) 
		  {
			  array_push($roleLogicNameArray,$roleItem->logic_name);
		  }

		  $rolesArray = $roleLogicNameArray;

		  $result = 0;

		  if(count($rolesArray))
		  {
			  $result =  Redis::hset("admin_roles:admin_roles",$admin->id,json_encode($rolesArray));
		  }

		  if(!$result)
		  {
			  plog(['RedisSaveAdminRolesError'=>'缓存管理员角色失败'],'AdminSystem','RedisSaveAdminRolesError');
		  }
	   }
	   
	   return $rolsArray;
   }
}

if(!function_exists('isDevelop'))
{
   /**
	* 是否是开发者
	*
	* @return boolean
	*/
   function isDevelop(Admin $admin)
   {
	   $rolesArray = getAdminRoles($admin);

	   $result = 0;

	   $result = \in_array('develop',$rolesArray);

	   return $result;
   }
}   

if(!function_exists('isSuper'))
{
   /**
	* 是否是超级管理员
	*
	* @return boolean
	*/
   function isSuper(Admin $admin)
   {
	   $rolesArray = getAdminRoles($admin);

	   $result = 0;
	   $developResult = \in_array('develop',$rolesArray);
	   $superResult = \in_array('super',$rolesArray);

	   if($developResult || $superResult )
	   {
		   $result = 1;
	   }

	   return $result;
   }
}
   

if(!function_exists('isAdmin'))
{
   /**
	* 是否是管理员
	*
	* @return boolean
	*/
   function isAdmin(Admin $admin)
   {
	   $rolesArray = getAdminRoles($admin);

	   $result = 0;

	   $developResult = \in_array('develop',$rolesArray);
	   $superResult = \in_array('super',$rolesArray);
	   $adminResult = \in_array('admin',$rolesArray);

	   if($developResult || $superResult || $adminResult)
	   {
		   $result = 1;
	   }

	   return $result;
   }
}

if(!function_exists('isAlbumAdmin'))
{
   /**
	* 是否是相册管理员
	*
	* @return boolean
	*/
   function isAlbumAdmin(Admin $admin)
   {
	   $rolesArray = getAdminRoles($admin);

	   $result = 0;

	   $developResult = \in_array('develop',$rolesArray);
	   $superResult = \in_array('super',$rolesArray);
	   $adminResult = \in_array('album_admin',$rolesArray);

	   if($developResult || $superResult || $adminResult)
	   {
		   $result = 1;
	   }

	   return $result;
   }
}

if(!function_exists('isOrderAdmin'))
{
   /**
	* 是否是相册管理员
	*
	* @return boolean
	*/
   function isOrderAdmin(Admin $admin)
   {
	   $rolesArray = getAdminRoles($admin);

	   $result = 0;

	   $developResult = \in_array('develop',$rolesArray);
	   $superResult = \in_array('super',$rolesArray);
	   $adminResult = \in_array('order_admin',$rolesArray);

	   if($developResult || $superResult || $adminResult)
	   {
		   $result = 1;
	   }

	   return $result;
   }
}

if(!function_exists('isArticleAdmin'))
{
   /**
	* 是否是文章管理员
	*
	* @return boolean
	*/
   function isArticleAdmin(Admin $admin)
   {
	   $rolesArray = getAdminRoles($admin);

	   $result = 0;

	   $developResult = \in_array('develop',$rolesArray);
	   $superResult = \in_array('super',$rolesArray);
	   $adminResult = \in_array('article_admin',$rolesArray);

	   if($developResult || $superResult || $adminResult)
	   {
		   $result = 1;
	   }

	   return $result;
   }
}
   

if(!function_exists('isUser'))
{
   /**
	* 是否是用户
	*
	* @return boolean
	*/
   function isUser(Admin $admin)
   {
	   $rolesArray = getAdminRoles($admin);

	   $result = 0;

	   $developResult = \in_array('develop',$rolesArray);
	   $superResult = \in_array('super',$rolesArray);
	   $adminResult = \in_array('admin',$rolesArray);
	   $userResult = \in_array('user',$rolesArray);

	   if($developResult || $superResult || $adminResult || $userResult)
	   {
		   $result = 1;
	   }

	   return $result;
   }
}
   